package com.social.socialservice.controller;

import com.social.socialservice.dal.EmailService;
import com.social.socialservice.dal.UserDAL;
import com.social.socialservice.model.FansListInfo;
import com.social.socialservice.model.UserInfo;
import com.social.socialservice.unit.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户相关请求API
 */
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    EmailService emailService;

    @Autowired
    UserDAL userDAL;

    /**
     * 测试发送验证码邮件 sendcode
     */
    @RequestMapping(value = "/sendcode", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response sendValidateCode(@RequestBody UserInfo userInfo) {
        return emailService.sendValidateCode(userInfo);
    }

    /**
     * 注册用户方法 regUser
     */
    @RequestMapping(value = "/regUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response regUser(@RequestBody UserInfo userInfo) {
        return userDAL.RegUser(userInfo);
    }

    /**
     * 用户登录方法 loginUser
     */
    @RequestMapping(value = "/loginUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response loginUser(@RequestBody UserInfo userInfo) {
        return userDAL.LoginUser(userInfo);
    }

    /**
     * 忘记密码
     * 发送验证邮件
     */
    @RequestMapping(value = "/sendreset", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response resetmail(@RequestBody UserInfo userInfo) {
        return emailService.resetAndSendMail(userInfo);
    }

    /**
     * 密码找回
     * 通过验证重置码，修改个人密码
     * 通过用户名或邮箱地址二选一，并且验证码要正确才可以修改
     */
    @RequestMapping(value = "/resetUserPass", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response resetUserPass(@RequestBody UserInfo userInfo) {
        return userDAL.resetUserPass(userInfo);
    }

    /**
     * 登录后
     * 根据user_id获取用户动态信息
     * 关注者/被关注者/个性简介等
     */
    @RequestMapping(value = "/getInfoByUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getInfoByUserId(@RequestBody UserInfo userInfo) {
        return userDAL.getInfoByUserId(userInfo);
    }


    /**
     * getIsFoByCurrentUserId
     * 与帖子转发、点赞状态一起，查询当前发帖的用户是否被当前登录用户关注
     */
    @RequestMapping(value = "/getIsFoByCurrentUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getIsFoByCurrentUserId(@RequestBody FansListInfo fansListInfo) {
        return userDAL.getIsForByCurrentUserId(fansListInfo);
    }

    /**
     * addUserFoPostUser
     * 关注用户
     * 关注用户前判断当前是否已经关注了用户，避免重复关注
     */
    @RequestMapping(value = "/addUserFoPostUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response addUserFoPostUser(@RequestBody FansListInfo fansListInfo) {
        return userDAL.addUserFoPostUser(fansListInfo);
    }

    /**
     * cancelUserFoPostUser
     * 取消关注用户
     */
    @RequestMapping(value = "/cancelUserFoPostUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response cancelUserFoPostUser(@RequestBody FansListInfo fansListInfo) {
        return userDAL.delUserFoPostUser(fansListInfo);
    }

    /**
     * updateUserAuth
     * 更新认证信息
     */
    @RequestMapping(value = "/updateUserAuth", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response updateUserAuth(@RequestBody UserInfo userInfo) {
        return userDAL.updateUserAuth(userInfo);
    }

    /**
     * getUserInfoByUserId
     * 重新获取当前用户基本信息，用于刷新
     */
    @RequestMapping(value = "/getUserInfoByUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getUserInfoByUserId(@RequestBody UserInfo userInfo) {
        return userDAL.getUserInfoByUserId(userInfo);
    }
}
